package com.example.yk2021xm04.controller;


import com.example.yk2021xm04.model.RespBean;
import com.example.yk2021xm04.pojo.Asset;
import com.example.yk2021xm04.pojo.User;
import com.example.yk2021xm04.query.AssetQuery;
import com.example.yk2021xm04.service.IAssetService;
import com.example.yk2021xm04.utils.IDUtils;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author LiYongle
 * @since 2021-11-30
 */
@Controller
@RequestMapping("/asset")
public class AssetController {

    @Resource
    private IAssetService assetService;

    /**
     * 资产管理主界面
     * @return
     */
    @RequestMapping("index")
    public String index() {
        return "asset/asset";
    }

    /**
     * 添加更新资产页面
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("addOrUpdateAssetPage")
    public String addOrUpdatePage(Integer id, Model model) {
        if (null!=id) {
            model.addAttribute("asset",assetService.getById(id));
        }
        return "asset/add_update";
    }

    @RequestMapping("list")
    @ResponseBody
    public Map<String, Object> assetList(AssetQuery assetQuery) {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        User user = (User) principal;
        return assetService.assetList(user.getId(),assetQuery);
    }

    @RequestMapping("delete")
    @ResponseBody
    public RespBean deleteAsset(Integer[] ids) {
        assetService.deleteAsset(ids);
        return RespBean.success("资产记录删除成功");
    }

    @RequestMapping("add")
    @ResponseBody
    public RespBean addAsset(Asset asset) {
        assetService.addAsset(asset);
        return RespBean.success("资产记录添加成功");
    }


    @RequestMapping("update")
    @ResponseBody
    public RespBean updateAsset(Asset asset) {
        assetService.updateAsset(asset);
        return RespBean.success("资产记录更新成功");
    }

    @RequestMapping("count")
    public String statistic() {
        return "asset/asset-statistics";
    }

    @RequestMapping("statistic")
    @ResponseBody
    public RespBean assetStatistic() {
        //发布时间分组 group by day
        List<Object> registerTimeByDay = assetService.buyTimeByDay();
        //一天中的发布任务数
        List<Object> registerCountByDay = assetService.buyCountByDay();
        //日同比
        double rate = 0.0;
        //总任务数量
        Integer totalAssetCount = assetService.count();
        //今天的发布任务数量
        Integer AssetCountInDay = assetService.AssetCountInDay();
        //昨天的发布任务数量
        Integer AssetCountInYesterday = assetService.AssetCountInYesterday();

        rate = (double)(AssetCountInDay - AssetCountInYesterday) / AssetCountInYesterday;
        if(AssetCountInYesterday == 0) {
            rate = 0;
        }
        Map<String, Object> res = new HashMap<>();
        res.put("num", registerCountByDay);
        res.put("time", registerTimeByDay);
        res.put("total", totalAssetCount);
        res.put("dayCount", AssetCountInDay);
        res.put("rate", rate);
        return RespBean.success("任务统计数据返回成功",res);
    }
}
